package com.glorysoft.lipuxinpda.dialog

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.InputType
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.annotation.Nullable
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.glorysoft.lipuxinpda.R
import com.glorysoft.lipuxinpda.callback.StringCommonCallBack
import com.glorysoft.lipuxinpda.utils.ButtonUtils
import com.pda.platform.ui.ui_pdaplatform.activity_public.FreeUI_QRCodeActivity
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DisplayUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils

/**
 * FileName: InputFragmentDialog
 * Author: XJH
 * Date: 2021/4/15 15:38
 * Description:
 * History:
 */
class InputFragmentDialog : AppCompatDialogFragment() {
    private var tips: String = ""
    private var leftStr: String = ""
    private var rightStr:String = ""
    private var text: String = ""
    private var onClickListener: View.OnClickListener? = null
    private var callBack: StringCommonCallBack? = null
    private var canelBack: FreeUI_CommonCallback? = null
    private var isPassWord = false
    private var etRemark: EditText? = null

    override fun show(manager: FragmentManager, tag: String?) {
        if (!isAdded) {
            val ft: FragmentTransaction = manager.beginTransaction()
            ft.add(this, tag)
            ft.commitAllowingStateLoss()
        }
    }

    override fun onStart() {
        super.onStart()
        val dialog = dialog
        if (dialog != null) {
            dialog.window!!.setLayout((FreeApi_DisplayUtils.getScreenWidth() * 0.8).toInt(), ViewGroup.LayoutParams.WRAP_CONTENT)
        }
    }

    fun setCallBackAndWhere(tips: String, callBack: StringCommonCallBack?, text: String) {
        this.callBack = callBack
        this.tips = tips
        this.text = text
    }

    fun setCallBackAndWhere(tips: String, callBack: StringCommonCallBack?, text: String, isPassWord: Boolean) {
        this.callBack = callBack
        this.tips = tips
        this.text = text
        this.isPassWord = isPassWord
    }

    fun setData(tips: String, confirmListener: View.OnClickListener?) {
        this.tips = tips
        onClickListener = confirmListener
    }

    fun setData(tips: String, leftStr: String, rightStr: String, confirmListener: View.OnClickListener?) {
        this.tips = tips
        onClickListener = confirmListener
        this.leftStr = leftStr
        this.rightStr = rightStr
    }

    fun setCallBack(canelBack: FreeUI_CommonCallback?) {
        this.canelBack = canelBack
    }

    fun clearData() {
        if (etRemark != null) etRemark!!.setText("")
    }

    @Nullable
    override fun onCreateView(inflater: LayoutInflater, @Nullable container: ViewGroup?, @Nullable savedInstanceState: Bundle?): View? {
        val view: View = inflater.inflate(R.layout.dialog_genaral, null)
        dialog!!.window!!.setGravity(Gravity.CENTER) // 非常重要：设置对话框弹出的位置
        dialog!!.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog!!.window!!.setWindowAnimations(R.style.BottomDialog) // 添加动画
        dialog!!.setCancelable(false)
        return view
    }

    override fun onViewCreated(view: View, @Nullable savedInstanceState: Bundle?) {
        val bAddOne = view.findViewById<Button>(R.id.button_scan) //扫描按钮
        val btnConfirm = view.findViewById<Button>(R.id.btnConfirm) //确认按钮
        val btnCancel = view.findViewById<Button>(R.id.btnCancel) //取消按钮
        etRemark = view.findViewById(R.id.etRemark) //dialog内容
        val tvTitle = view.findViewById<TextView>(R.id.tvTitle)
        tvTitle.text = tips
        etRemark?.setText(text)
        etRemark?.text?.length?.let { etRemark?.setSelection(it) }
        if (leftStr != null && leftStr.isNotEmpty()) {
            btnCancel.text = leftStr
        }
        if (rightStr != null && rightStr.isNotEmpty()) {
            btnConfirm.text = rightStr
        }
        /*判断通用弹框，如果Title为请输入密码时，不显示扫描按钮，影藏*/if (tvTitle.text.toString().trim { it <= ' ' } == "请输入密码" || tvTitle.text.toString().trim { it <= ' ' } == "请输入IP地址" || tvTitle.text.toString().trim { it <= ' ' } == "请输入超时时间") {
            bAddOne.visibility = View.GONE
        } else {
            bAddOne.visibility = View.VISIBLE
        }
        //扫描按钮点击事件
        bAddOne.setOnClickListener {
            back(context)
        }

        //取消按钮点击事件
        btnCancel.setOnClickListener { dismiss() }
        //确认按钮点击事件
        btnConfirm.setOnClickListener(View.OnClickListener { v ->
            if (ButtonUtils().isFastDoubleClick(v.id)) {
                FreeApi_ToastUtils.showFreeToast(getString(R.string.doNotClickRepeatedly), activity, true, 0)
                return@OnClickListener
            }
            if (etRemark?.text.toString().trim { it <= ' ' }.isEmpty()) {
                FreeApi_ToastUtils.showFreeToast(getString(R.string.enterTheContent), activity, true, 0)
            } else {
                dismiss()
                callBack?.onCallback(etRemark?.text.toString().trim { it <= ' ' })
            }
        })
        if (isPassWord) {
            etRemark?.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        }
        super.onViewCreated(view, savedInstanceState)
    }

    private fun back(context: Context?) {
        val intent = Intent(context, FreeUI_QRCodeActivity::class.java)
        startActivityForResult(intent, 103)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == -1) {
            if (data != null) {
                val barCode = data.getStringExtra("barCode")
                etRemark!!.setText(barCode)
                etRemark!!.setSelection(barCode!!.length)
            }
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        if (canelBack != null) {
            canelBack!!.onSuccess()
        }
    }
}